package sjs.util;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class AccessResult {
	private List<Map<String,String>> _rsListM;
	private List<List<String>> _rsListL;
	private int rowSize=0;
	private int columnCount=0;
	public AccessResult(){}
	public AccessResult(ResultSet rs)throws SQLException{
			_rsListM=new LinkedList<Map<String,String>>();
			_rsListL=new LinkedList<List<String>>();
		 	ResultSetMetaData rsm=rs.getMetaData();
	 		columnCount=rsm.getColumnCount();
		 	if(rs.next()){
		 		rowSize++;
	           Map<String,String> rowMap=new LinkedHashMap<String, String>();
	           List<String> rowList=new LinkedList<String>();
	           for(int i=0;i<columnCount;i++){
	        	   String columnLabel=rsm.getColumnLabel(i+1);
	        	   String columnValue=rs.getString(columnLabel);
	        	   rowMap.put(columnLabel,columnValue);
	        	   rowList.add(columnValue);
	           }
	           _rsListM.add(rowMap);
	           _rsListL.add(rowList);
	        }
	}
	/**
	 * @param rowindex
	 * @return 
	 */
	public Map<String,String> getRowMap(int rowindex){
		if(rowindex>=this.rowSize)return null;
		return this._rsListM.get(rowindex);
	}
	/**
	 * @param rowindex
	 * @return
	 */
	public List<String> getRowList(int rowindex){
		if(rowindex>=this.rowSize)return null;
		return this._rsListL.get(rowindex);
	}
	/**
	 * 
	 * @param rowindex
	 * @param columnindex
	 * @return
	 */
	public String get(int rowindex,int columnindex){
		if(rowindex>=this.rowSize)return null;
		if(columnindex>=this.columnCount)return null;
		return this.getRowList(rowindex).get(columnindex);
	}
	/**
	 * 
	 * @param rowindex
	 * @param columnLabel
	 * @return
	 */
	public String get(int rowindex,String columnLabel){
		if(rowindex>=this.rowSize)return null;
		return getRowMap(rowindex).get(columnLabel);
	}
	public int getRowSize() {
		return rowSize;
	}
	public int getColumnCount() {
		return columnCount;
	}
	
}
